#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

int main(int argc, char const* argv[])
{
    int fd, i;
    char buf[10];

    if (argc != 2)
    {
        fprintf(stderr, "usage: %s <filename>\n", argv[0]);
        exit(0);
    }

    fd = open(argv[1], O_RDWR|O_APPEND);
    if (fd < 0)
    {
        perror("open error");
        exit(0);
    }

    if (lseek(fd, 1, SEEK_SET) < 0)
    {
        perror("lseek error");
        exit(0);
    }

    read(fd, buf, 4);
    for (i = 0; i < 4; i++) {
        printf("%c\n", buf[i]);
    }

    if (lseek(fd, 5, SEEK_SET) < 0)
    {
        perror("lseek error");
        exit(0);
    }

    write(fd, "changed", 7);

    close(fd);

    return 0;
}
